
package com.yimixian.app.cart;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.CartUpdate;
import com.yimixian.app.model.Store;
import com.yimixian.app.receiver.ButtonClickTransHelper;
import com.yimixian.app.ui.AutoMoreTextLinearlayout;
import com.yimixian.app.ui.ProgressDialogFragment;
import com.yimixian.app.util.UiUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CartNewView extends FrameLayout {

    private Adapter mAdapter;

//    @InjectView(R.id.cart_tv_total_price)
//    TextView mCartTotalPrice;
//    @InjectView(R.id.cart_tv_isFreeSend)
//    TextView mIsFreeSend;
    @InjectView(R.id.bar_button)
    TextView mSelectOK;
    @InjectView(R.id.auto_more_text_lin)
    AutoMoreTextLinearlayout mAutoMoreTextLinearlayout;

    public interface Adapter {
        void onButtonClicked();

        BaseAdapter getCartListAdapter();
    }

    public CartNewView(Context context) {
        super(context);
        initView();
    }

    public CartNewView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CartNewView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.cart_view_new, this);
        ButterKnife.inject(this);

        mSelectOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(UiUtils.isFastDoubleClick()) {
                    return;
                }
                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_CART_ACCOUNTBTN);
                if (mAdapter != null) {
                    mAdapter.onButtonClicked();
                }
            }
        });
    }

    public void setAdapter(Adapter adapter) {
        mAdapter = adapter;
    }

    public void setTotalPrice(CartUpdate mCartUpdate) {

        if (mCartUpdate != null && mCartUpdate.total != null) {
            mAutoMoreTextLinearlayout.setInitData(mCartUpdate.total.desc, "#666666", 13);
        }
//        if(mCartTotalPrice != null) {
//            mCartTotalPrice.setText(price + "元");
//        }
    }

//    public void setIsFreeSend(float price) {
//        if(CartManager.getInstance().mCartUpdate != null && CartManager.getInstance().mCartUpdate.total != null && mIsFreeSend != null) {
//            if(CartManager.getInstance().mCartUpdate.total.desc != null && CartManager.getInstance().mCartUpdate.total.desc.size() > 2) {
//                if(price < 0.005) {
//                    mIsFreeSend.setVisibility(View.GONE);
//                } else {
//                    mIsFreeSend.setVisibility(View.VISIBLE);
//                    mIsFreeSend.setText(CartManager.getInstance().mCartUpdate.total.desc.get(2).content);
//                }
//            } else {
//                mIsFreeSend.setVisibility(View.GONE);
//            }
//        }
//    }

    public void setSelectOkClickable(boolean clickable) {
        if (clickable) {
            mSelectOK.setEnabled(true);
            mSelectOK
                    .setBackgroundResource(R.drawable.address_new_selector);
        } else {
            mSelectOK.setEnabled(false);
            mSelectOK
                    .setBackgroundResource(R.color.cancel_button_color);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

}
